package org.geekbang.dbexcel.Service.DbService.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.geekbang.dbexcel.Service.DbService.DbService;
import org.geekbang.dbexcel.domain.Column;
import org.geekbang.dbexcel.domain.Table;
import org.geekbang.dbexcel.mapping.DbMapping;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 孙红瑞
 * @ClassName DbServiceImpl.java
 * @Description TODO
 * @createTime 2021年02月03日 15:43:00
 */
@Slf4j
@Service
public class DbServiceImpl implements DbService {

    @Resource
    DbMapping dbMapping;
    /**
     * 获取数据库所有表
     * @return
     */
    @Override
    public List<Table> listTable() {
        List<Map<String,String>> list = dbMapping.listTable();
        String json = JSON.toJSONString(list);
        List<Table> tables = JSON.parseArray(json,Table.class);
        return tables;
    }

    /**
     * 根据表名获取表信息
     * @param tableName
     * @return
     */
    @Override
    public Table tableInfo(String tableName) {
        Map<String,String> map = dbMapping.tableInfo(tableName);
        String json = JSON.toJSONString(map);
        Table tableInfo = JSON.parseObject(json,Table.class);
        return tableInfo;
    }

    /**
     * 根据表名获取字段信息
     * @param tableName
     * @return
     */
    @Override
    public List<Column> listTableColumn(String tableName) {
        List<Map> list =  dbMapping.listTableColumn(tableName);
        log.info("{} 一共有 {} 个字段",tableName,list.size());
        log.info("字段为：{}",list.toString());
        String json = JSON.toJSONString(list);
        List<Column> columns = JSON.parseArray(json,Column.class);
        log.info("具体字段为：{}",columns.toString());
        return columns;
    }

    /**
     * 获取建表语句
     * @param tableName
     * @return
     */
    @Override
    public String createSql(String tableName) {
        // Table  Create Table
        Map<String,String> map = dbMapping.createSql(tableName);
        log.info("建表sql:{}",map.get("Create Table"));
        return map.get("Create Table");
    }

    /**
     * 获取表注释，生成表注释Sql语句
     *
     * @param tableName
     * @return
     */
    @Override
    public String alterTableComment(String tableName) {
        Table table = tableInfo(tableName);
        StringBuilder sb = new StringBuilder();
        sb.append("ALTER TABLE ").append(tableName).append(" COMMENT ").append("'"+ table.getTableComment() +"';").append("\n");
        return sb.toString();
    }

    /**
     * 获取表字段注释，生成表注释SQL语句
     * @param tableName
     * @return
     */
    @Override
    public String alterColumnComment(String tableName) {
        List<Column> columns = listTableColumn(tableName);
        StringBuilder sb = new StringBuilder();
        for(int j=0;j<columns.size() ;j++){
            if(StringUtils.isBlank(columns.get(j).getColumnKey())){
                sb.append("ALTER TABLE ").append(tableName).append("  MODIFY COLUMN  ")
                        .append(columns.get(j).getColumnName()).append("  " + columns.get(j).getColumnType() + "  ");
                String isNull = columns.get(j).getIsNullable();
                if(isNull.equals("YES")){
                    sb.append("NOT NULL");
                }else{
                    sb.append("DEFAULT NULL");
                }
                sb.append(" COMMENT ").append("'"+columns.get(j).getColumnComment()+"';").append("\n");
            }
        }
        return sb.toString();
    }
}
